package com.aquarium.ui.controller;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.aquarium.R;
import com.aquarium.data.ModuleSettingDataBase;
import com.aquarium.module.ModuleControlService;

public class WaterMotorSetting extends ControllerSetting {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mTitle.setText(getResources().getString(R.string.water_motor));
	}

	@Override
	protected void onResume() {
		super.onResume();
		invalidateListView();
	}

	@Override
	public void createListViewAdapter() {
		mTimePickListViewAdapter = new TimePickListViewAdapter(this,
				R.layout.water_moter_setting_row,
				mModuleSettingDataBase
						.getCursor(ModuleSettingDataBase.WATER_MOTOR_TABLE),
				new String[] { ModuleSettingDataBase.HOUR,
						ModuleSettingDataBase.MINUTE,
						ModuleSettingDataBase.ON_OFF,
						ModuleSettingDataBase.MON, ModuleSettingDataBase.TUES,
						ModuleSettingDataBase.WED, ModuleSettingDataBase.THUR,
						ModuleSettingDataBase.FRI, ModuleSettingDataBase.SAT,
						ModuleSettingDataBase.SUN }, new int[] {
						R.id.start_time_txv, R.id.on_off_txv, R.id.mon_txv,
						R.id.tues_txv, R.id.wed_txv, R.id.thur_txv,
						R.id.fri_txv, R.id.sat_txv, R.id.sun_txv });
	}

	@Override
	public void setListeners() {
		super.setListeners();
		mAddButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ACTION_ADD_SETTING);
				intent.setClass(WaterMotorSetting.this,
						WaterMotorTimePicker.class);
				startActivity(intent);
			}
		});
		mDeleteButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				mModuleSettingDataBase.deleteWaterMotorData(mDeleteList);
				invalidateListView();
				Intent intent = new Intent(
						ModuleControlService.ACTION_UPDATE_WATER_MOTOR_DATA);
				sendBroadcast(intent);
			}
		});

		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterview, View view,
					int position, long id) {
				Intent intent = new Intent(ACTION_MODIFY_SETTING);
				intent.setClass(WaterMotorSetting.this,
						WaterMotorTimePicker.class);
				intent.putExtra(INTENT_EXTRA_LISTVIEW_CLICKED_POSITION, position);
				startActivity(intent);
			}
		});
	}

	@Override
	public void setListViewBinder() {

		mTimePickListViewAdapter
				.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
					@Override
					public boolean setViewValue(View view, Cursor cursor,
							int columnIndex) {
						boolean result = bindListView(view, cursor);
						int id = view.getId();
						if (id == R.id.on_off_txv) {
							boolean isOn = mModuleSettingDataBase
									.isWaterMotorOn(cursor);
							if (isOn) {
								((TextView) view).setText(getResources()
										.getString(R.string.open));
							} else {
								((TextView) view).setText(getResources()
										.getString(R.string.close));
							}
							return true;
						}
						return result;
					}
				});
	}

	private void invalidateListView() {
		mTimePickListViewAdapter.changeCursor(mModuleSettingDataBase
				.getCursor(ModuleSettingDataBase.WATER_MOTOR_TABLE));
		mListView.invalidate();
	}
}
